using System;

namespace Natrue.Data.InitHelper
{
    public class JavaScrtiptBuilder : CodeBuilder
    {
        public string ToString(bool addScriptTag)
        {
            return string.Format("<script type=\"text/javascript\">\r\n<!--\r\n{0}\r\n//-->\r\n</script>", base.ToString());
        }
    }

    public class JavaScriptFunction :System.IDisposable
    {
        private readonly JavaScrtiptBuilder JsBuilder;

        public JavaScriptFunction(JavaScrtiptBuilder jsBuilder, string functionName, params string[] param)
        {
            JsBuilder = jsBuilder;
            string paraStr = String.Empty;
            for (int i = 0; i < param.Length; i++)
            {
                paraStr += param[i];
                if ((i + 1) < param.Length) paraStr += ", ";
            }
            JsBuilder.Append(String.Format("function {0}({1}){{", functionName, paraStr));
        }

        public JavaScriptFunction Append(string javaScript)
        {
            JsBuilder.Append(javaScript);
            return this;
        }

        #region Implementation of IDisposable

        public void Dispose()
        {
            JsBuilder.Append("}");
        }

        #endregion
    }
}